package com.hitqz.robot.driver.netty.handler;

import cn.hutool.core.util.StrUtil;
import com.hitqz.robot.driver.util.TasLan464Toolkit;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @author xupkun
 * @date 2023/8/4
 *
 * 万华用的传感器
 */
@Slf4j
@ChannelHandler.Sharable
public class SensorHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private String strategy = "华瑞";

    public SensorHandler(String strategy){
        if (StrUtil.isNotBlank(strategy)) {
            this.strategy = strategy;
        }
    }


    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        String name = channelHandlerContext.name();
        BigDecimal value;
        if (this.strategy.equals("山盾")) {
            value = this.sunDoDecode(byteBuf);
        } else {
            //万华的
            value = this.defaultDecode(byteBuf);
        }

        if (value == null){
            return;
        }

        if (name.contains(",")) {
            String[] split = name.split(",");
            name = split[Math.min(TasLan464Toolkit.sn, split.length - 1)];
            if (TasLan464Toolkit.sn==0){
                TasLan464Toolkit.sn=1;
            }else if(TasLan464Toolkit.sn==1){
                TasLan464Toolkit.sn=0;
            }
        }

        log.debug("[sensor] {}:{} ", name, value);
        if (Objects.equals(name,"温度")){
            TasLan464Toolkit.cache.put(name, value + "℃");
            return;
        }
        if (Objects.equals(name,"湿度")){
            TasLan464Toolkit.cache.put(name, value + "%");
            return;
        }
        if (Objects.equals(name,"可燃气") || Objects.equals(name,"可燃气体") || Objects.equals(name,"氢气")){
            TasLan464Toolkit.cache.put(name, value + "%LEL");
            return;
        }
        TasLan464Toolkit.cache.put(name, value + "ppm");

    }


    private BigDecimal sunDoDecode(ByteBuf byteBuf){
        // 确保缓冲区足够 9 字节
        if (byteBuf.readableBytes() < 9) {
            return null;
        }

        byte[] frame = new byte[3];
        byteBuf.readBytes(frame);

        // 校验是否是我们要的协议格式
        if (frame[0] != 0x01 || frame[1] != 0x03 || frame[2] != 0x04 ) {
            return null;
        }

        // 提取数据
        float data = byteBuf.readFloat();

        return BigDecimal.valueOf(data).setScale(1, RoundingMode.HALF_UP);

    }

    private BigDecimal defaultDecode(ByteBuf byteBuf){
        // 确保缓冲区足够 9 字节
        if (byteBuf.readableBytes() < 9) {
            return null;
        }

        byte[] frame = new byte[9];
        byteBuf.readBytes(frame);

        // 校验是否是我们要的协议格式
        if (frame[0] != 0x01 || frame[1] != 0x03 || frame[2] != 0x04 || frame[3] != 0x00 || frame[4] != 0x00) {
            return null;
        }

        // 提取数据（高低位合并）
        int data = ((frame[5] & 0xFF) << 8) | (frame[6] & 0xFF);

        return BigDecimal.valueOf(data).multiply(BigDecimal.valueOf(0.001)).setScale(1, RoundingMode.HALF_UP);

    }



}
